<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>猜数字小游戏</title>
</head>
<body>
<input type = "text" name = "T1" id="t1" size = "1" maxlength="1" style="width: 25px; height: 25PX;" onkeyup="return T1_onkeyup()">
<input type = "text" name = "T2" id="t2" size = "1" maxlength="1" style="width: 25px; height: 25PX;" onkeyup="return T2_onkeyup()">
<input type = "text" name = "T3" id="t3" size = "1" maxlength="1" style="width: 25px; height: 25PX;" onkeyup="return T3_onkeyup()">
<input type = "text" name = "T4" id="t4" size = "1" maxlength="1" style="width: 25px; height: 25PX;">
<button onclick="check()">检验猜的结果</button><br><br>
<script>
	
    var num1 = new Array(4);
    for(var i = 0; i < 4;i++) {
            num1[i] = parseInt(Math.random()*9,10)+1;
            for(var j = 0; j < i; j++) {
                if(num1[i] == num1[j]){
                    i = j;
                }
            }
        }

    
    function check() {
                
        var num2 = new Array(4);
        var n1 = parseInt(document.getElementById("t1").value);
        var n2 = parseInt(document.getElementById("t2").value);
        var n3 = parseInt(document.getElementById("t3").value);
        var n4 = parseInt(document.getElementById("t4").value);
        num2[0] = n1;
        num2[1] = n2;
        num2[2] = n3;
        num2[3] = n4;
        
        
        var result = "";
        var r1 = 0;
        var r2 = 0;
        for(var i = 0;i < 4;i++) {
            for(var j = 0;j < 4;j++) {
                if(num1[i] == num2[j] && i != j) {r2++;}
                else if(num1[i] == num2[j] && i == j) {r1++;}
            }
        }
        result = r1 + "a" + r2 + "b";
        
        var table = document.createElement("table");
        var tr = document.createElement("tr");
        
        var td1 = document.createElement("td");
        var td2 = document.createElement("td");
        var td3 = document.createElement("td");
        var td4 = document.createElement("td");
        var td5 = document.createElement("td");
        var td6 = document.createElement("td");
        
        
        
        td1.appendChild(document.createTextNode(String(n1)));
        td2.appendChild(document.createTextNode(String(n2)));
        td3.appendChild(document.createTextNode(String(n3)));
        td4.appendChild(document.createTextNode(String(n4)));
        td5.appendChild(document.createTextNode(result));
        td6.appendChild(document.createTextNode(num1[0] + "" + num1[1] + "" + num1[2] + "" + num1[3]));

        
        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);
        tr.appendChild(td4);
        tr.appendChild(td5);
        tr.appendChild(td6);
        
        table.appendChild(tr);
        table.setAttribute("border",1);
        
        document.body.appendChild(table);
    }
</script>
</body>
</html>